package com.itic.hpp.user;

import com.itic.appbase.framework.Base.BaseCode;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mobile.base.BaseMobile;
import com.itic.system.user.withdraw.persistence.model.UserWithDrawRecord;
import com.itic.system.user.withdraw.persistence.model.UserWithDrawRecordExample;
import com.itic.system.user.withdraw.service.UserWithDrawRecordService;
import com.itic.system.vo.WithDrawListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/hpp/user/withdraw")
public class UserWithDrawRecordInterface extends BaseMobile {

    @Autowired
    private UserWithDrawRecordService userWithDrawRecordService;

    /**
     * 申请提现
     * @param userId 用户ID
     * @param money 提现金额
     * @return
     */
    @RequestMapping(value = "/applyWithDraw")
    @ResponseBody
    public Map<String, Object> applyWithDraw(String userId,Double money) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            // 效验参数
            if (StringHelper.isNoneBlank(userId) && money !=null) {
                resultMap = userWithDrawRecordService.applyWithDrawRecord(userId,money);
            } else {
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        } catch (Exception e) {
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("UserInterface", "applyWithDraw", e.toString(), "申请提现接口", e);
        }
        return resultMap;
    }

    /**
     * 提现列表
     * @param userId
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> withDrawList(String userId, HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            // 效验参数
            if (StringHelper.isNoneBlank(userId)) {
                UserWithDrawRecordExample example = new UserWithDrawRecordExample();
                this.handlePageQueryDefault(request,example);
                List<UserWithDrawRecord> userWithDrawRecords = this.userWithDrawRecordService.findUserWithDrawRecordByPageAndUserId(userId,example);
                List<WithDrawListVo> withDrawListVos = new ArrayList<>();
                for(UserWithDrawRecord userWithDrawRecord : userWithDrawRecords){
                    WithDrawListVo withDrawListVo = this.userWithDrawRecordService.UserWithDrawRecordWrapToWithDrawListVo(userWithDrawRecord);
                    withDrawListVos.add(withDrawListVo);
                }
                resultMap.put(BaseCode.DATA, withDrawListVos);
                resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
            } else {
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        } catch (Exception e) {
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("UserInterface", "withDrawList", e.toString(), "提现列表接口", e);
        }
        return resultMap;
    }


}
